import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { wxMinLoginParams } from '@/types/login'
export const useUserStore = defineStore(
  'userStore',
  () => {
    // 用户信息的ts类型
    type userInfoType = {
      avatar: string
      account: string
      nickname: string
    }
    // token
    let token = ref<string>('')
    // 存储token
    const setToken = (val: string) => {
      token.value = val
    }
    // 清空token 
    const clearToken = () => {
      token.value = ''
    }
    // 用户信息
    const userInfo = ref<userInfoType>({} as userInfoType)
    // 存储用户信息
    const setUserInfo = (data: userInfoType) => {
      userInfo.value = data
    }
    // 清空用户信息
    const clearUserInfo = () => {
      userInfo.value = {} as wxMinLoginParams
    }
    return {
      token,
      setToken,
      clearToken,
      setUserInfo,
      userInfo,
      clearUserInfo,
    }
  },
  {
    // uni-app的持久化存储方式
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, val) {
          uni.setStorageSync(key, val)
        },
      },
    },
  },
)
